package com.zh.springcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zh.springcloud.common.Result;
import com.zh.springcloud.entity.Account;
import com.zh.springcloud.mapper.AccountMapper;
import com.zh.springcloud.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description:
 * @ClassName AccountServiceImpl
 * @date: 2021.08.12 18:47
 * @Author: zhanghang
 */
@Service
@Slf4j
public class AccountServiceImpl  implements AccountService {
	@Autowired
	private AccountMapper accountMapper;

	/**
	 * description: 扣减余额
	 * date: 2021年-08月-12日 18:48
	 * author: zhanghang
	 * 
	 * @param userId
 * @param money
	 * @return com.zh.springcloud.common.Result<?>
	 */ 
	@Override
	public Result<?> decrease(Long userId, BigDecimal money) {
		// 根据用户id，查询账户信息
		Account account = accountMapper.selectOne(new QueryWrapper<Account>().lambda().eq(Account::getUserId, userId));
		log.info("---> account-service 扣减账户余额开始");
		account.setUsed(account.getUsed().add(money));
		account.setResidue(account.getResidue().subtract(money));
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		accountMapper.updateById(account);
		log.info("---> account-service 扣减账户余额开始");
		return Result.ok();
	}
}
